package com.example.demo.controller;

import com.example.demo.model.Teacher;
import com.example.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
public class TeacherController {
    @Autowired
    private TeacherService teacherService;

    @RequestMapping("list")
    public String list(ModelMap map ,Teacher teacher){
        if ("".equals(teacher.getName())){
            teacher.setName(null);
        }
        map.put("teachers",teacherService.findAll(teacher));
        return "list";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable String id){
//        System.out.println("id:"+id);
        teacherService.deleteById(id);
        return "redirect:/list" ;
    }
    @RequestMapping("create")
    public String create(){
        return "create";
    }
    @RequestMapping("save")
    public String save(Teacher s){
//        System.out.println(s.getName());
        if (s.getId()==null){
            s.setId(UUID.randomUUID().toString());
        }
        teacherService.save(s);
        return "redirect:/list";
    }
    @RequestMapping("update/{id}")
    public String update(@PathVariable String id,ModelMap map){
        Teacher s=teacherService.getReferenceById(id);
        map.put("teacher",s);
        return "update";
    }

}
